package day02;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/2 20:14
 * 力扣 21
 */
public class LinkedListPractice06 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode s = new ListNode(-1, null);
        ListNode p = s;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                p.next = list1;
                list1 = list1.next;
            } else {
                p.next = list2;
                list2 = list2.next;
            }
            p = p.next;
        }
        if (list1 == null) {
            p.next = list2;
        } else {
            p.next = list1;
        }
        return s.next;
    }

    public ListNode mergeTwoLists02(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        if (list1.val < list2.val) {
            list1.next = mergeTwoLists02(list1.next, list2);
            return list1;
        } else {
            list2.next = mergeTwoLists02(list1, list2.next);
            return list2;
        }
    }
}
